package com.wsd.smartcarwasher.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.constans.AjaxJson;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.*;
import com.wsd.smartcarwasher.service.TChargeRecordService;
import com.wsd.smartcarwasher.service.TChargeService;
import com.wsd.smartcarwasher.service.WxPayService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.CommonUtil;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "余额相关的功能模块")
@Controller
@CrossOrigin
public class BalanceController {

    @Resource
    private TChargeService chargeService;
    @Resource
    private TChargeRecordService chargeRecordService;
    @Resource
    private WxService wxService;
    @Resource
    private WxPayService wxPayService;
    /**
     * 查询当前可用的充值方案
     * @param request
     * @return
     */
    @GetMapping("/v1/chargeQueryPlan")
    @ResponseBody
    public AjaxJson queryChargeList(HttpServletRequest request)
    {
        String sessionId = request.getHeader("authorization");
        UserInfo user = this.wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return AjaxJson.error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        List<Map<String, Object>> chargeList=this.chargeService.queryPlan();
        Map<String, Object> responseMap=new HashMap<>();
        responseMap.put("chargePlan",chargeList);
        responseMap.put("balance",StringUtil.isNullOrEmpty(user.getBalance())? BigDecimal.ZERO:user.getBalance());
        return AjaxJson.success().put("data",responseMap);
    }

    /**
     * 查询当前可用的充值方案
     * @param request
     * @return
     */
    @PostMapping("/v1/charge/pay")
    @ResponseBody
    public AjaxJson queryChargePay(@RequestBody Map<String, Object> map,HttpServletRequest request)
    {
        String sessionId = request.getHeader("authorization");
        UserInfo user = this.wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return AjaxJson.error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        if(StringUtil.isNullOrEmpty(map.get("chargeId")))
        {
            return AjaxJson.error("充值选项的id不可为空！！");
        }
        Integer chargeId=Integer.parseInt(map.get("chargeId").toString());
        TCharge charge=this.chargeService.getChargeById(chargeId);
        if(charge==null)
        {
            return AjaxJson.error("没有查询到当前的充值方案哦！！");
        }
        TChargeRecord tChargeRecord=new TChargeRecord();
        tChargeRecord.setUserid(user.getUserId());
        tChargeRecord.setCreatetime(new Date());
        tChargeRecord.setChargeid(chargeId);
        tChargeRecord.setPaystatus(1);
        tChargeRecord.setPrice(charge.getChargePrice());
        tChargeRecord.setRealPrice(charge.getChargePrice().add(charge.getChargeGiving()).setScale(2));
        System.out.println(DateUtil.now()+"充值订单,准备付钱"+tChargeRecord.getPrice());
        System.out.println(DateUtil.now()+"充值订单,预计到账"+tChargeRecord.getRealPrice());
        tChargeRecord.setPaystatus(1);
        tChargeRecord.setStatus(1);
        tChargeRecord.setType(1);
        String  transactionNo=IdUtil.simpleUUID();
        tChargeRecord.setTransactionNo(transactionNo);
        this.chargeRecordService.insert(tChargeRecord);
        String clientIP = CommonUtil.getClientIp(request);
        return AjaxJson.success().put("data",this.wxPayService.payByWxForCharge(user.getOpenid(), clientIP, tChargeRecord));
    }


    /**
     * 用户查询当前的余额的帐单明细
     * @param request
     * @return
     */
    @GetMapping("/v1/charge/record")
    @ResponseBody
    public AjaxJson chargeRecordList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                     HttpServletRequest request)
    {
        String sessionId = request.getHeader("authorization");
        UserInfo user = this.wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return AjaxJson.error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        List<Map<String, Object>> chargeRecordList=this.chargeRecordService.queryChargeRecord(user.getUserId());
        return AjaxJson.success().put("data",new Pagination<>(pageNum,pageSize,chargeRecordList));
    }




}
